package com.itheima.controller;

import com.itheima.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    UserManager userManager;

    //用户列表
    @GetMapping("/manage/users")
    public ResponseEntity findByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
       return userManager.findByPage(pageNum,pageSize);
    }

    //用户详情
    @GetMapping("/manage/users/{id}")
    public ResponseEntity findUserInfoById(@PathVariable("id") Long userId){
        return userManager.findUserInfoById(userId);
    }

    @GetMapping("/manage/messages")
    public ResponseEntity findMovementByCondition4Page(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
            @RequestParam(value = "uid",required = false)Long userId,
            @RequestParam(required = false)String state){

        /*
        state 有可能是"",null,"''",0,1,2
         */
        Integer status = null;

        if (state == null || "".equals(state) || "''".equals(state) ) {
            status = null;
        }else{
            status = Integer.parseInt(state);
        }


        return userManager.findMovementByCondition4Page(userId,status,pageNum,pageSize);
    }

    //动态详情
    @GetMapping("/manage/messages/{movementId}")
    public ResponseEntity findMovementById(@PathVariable String movementId){
        return userManager.findMovementById(movementId);
    }

    //动态评论列表
    @GetMapping("/manage/messages/comments")
    public ResponseEntity findMovementComemntByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
            @RequestParam("messageID") String movementId){
        return userManager.findMovementComemntByPage(movementId,pageNum,pageSize);
    }

    //用户冻结
    @PostMapping("/manage/users/freeze")
    public ResponseEntity Freeze(@RequestBody Map<String,String> map){
        //接受5个参数
        String userId = map.get("userId");
        String freezingTime = map.get("freezingTime");
        String freezingRange = map.get("freezingRange");
        String reasonsForFreezing = map.get("reasonsForFreezing");
        String frozenRemarks = map.get("frozenRemarks");

        return userManager.Freeze(userId,freezingTime,freezingRange,reasonsForFreezing,frozenRemarks);

    }


    //用户解冻
    @PostMapping("/manage/users/unfreeze")
    public ResponseEntity UnFreeze(@RequestBody Map<String,String> map){
        //接受2个参数
        String userId = map.get("userId");
        String reasonsForThawing = map.get("reasonsForThawing");
        return userManager.UnFreeze(userId,reasonsForThawing);
    }


    //动态通过
    @PostMapping("/manage/messages/pass")
    public ResponseEntity pass(@RequestBody List<String> item){
        return userManager.pass(item);
    }

    //动态拒绝
    @PostMapping("/manage/messages/reject")
    public ResponseEntity reject(@RequestBody List<String> item){
        return userManager.reject(item);
    }



}
